<?php

namespace app\index\controller;

/**
 * CateAdmin 用户分类管理
 *
 * @author Dongasai
 */
class CateAdmin extends \think\Controller
{

    /**
     *
     */
    public function index()
    {
        $group_id = input("group_id", 0, 'int');
        $uid      = session('uid');
        $data     = \app\index\service\Cate::getRecursionList($group_id);
        $this->assign('cate', $data);
        return $this->fetch();
    }

    /**
     * 增加分类
     */
    public function add()
    {
        $group_id = input("group_id", 0, 'int');
        if (!$group_id) {
            return $this->error("缺少必要参数!");
        }
        if (request()->ispost()) {
            //处理表单
            $data             = [];
            $data['group_id'] = input('group_id', 0, 'int');
            $data['name']     = input('name', '', 'string');
            $data['pid']      = input('pid', 0, 'int');
            $uid              = session('uid');
            $re               = \app\index\service\Cate::add($uid, $data);
            if (is_string($re)) {
                return $this->error($re);
            } else {
                return $this->success("成功!");
            }
        } else {
//            展示数据
            //读取分组信息
            $groupInfo = \app\index\service\Group::info($group_id);
            $this->assign('groupInfo', $groupInfo);
            //读取 该分组 父级分类
            $cate_list = \app\index\service\Cate::getRecursionList($groupInfo['id'], 0);
//            dump($cate_list);
            $this->assign('cate_list', $cate_list);
            return $this->fetch();
        }
    }

    public function edit()
    {
        $cate_id = input('cate_id', 0, 'int');
        $uid     = session('uid');
        if (!$cate_id) {
            return $this->error("缺少必要参数");
        }
        if (request()->ispost()) {
            dump($_POST);
            $to_id        = input('to_id', 0, 'int');
            $data         = [];
            $data['pid']  = input('pid', 0, 'int');
            $data['name'] = input('name', '', 'string');

            $re = \app\index\service\Cate::edit($uid, $to_id, $data);
            if (is_string($re)) {
                return $this->error($re);
            } else {
                return $this->success("成功!");
            }
        } else {
            //展示页面
//             读取分类信息
            $cateInfo = \app\index\service\Cate::info($cate_id);
            $this->assign('cateInfo', $cateInfo);

            ////读取 该分组 父级分类
            $cate_list = \app\index\service\Cate::getRecursionList($cateInfo['group_id'], 0);
//            dump($cate_list);
            $this->assign('cate_list', $cate_list);


            return $this->fetch();
        }
    }

    /**
     * 删除分类
     */
    public function del()
    {
        $cate_id = input('cate_id', 0, 'int');
        $uid     = session('uid');
        if (!$cate_id) {
            return $this->error("缺少必要参数");
        }
        $re = \app\index\service\Cate::del($uid, $cate_id);
        if (is_string($re)) {
            return $this->error($re);
        } else {
            return $this->success("成功!");
        }
    }

}
